package com.yxh.www.redis.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author yangxiaohui
 * @since 2020/5/7
 */
public class RedisResObjectUtil {

    public static <T> T resToObjectByClass(Object object,Class<T> tClass){
        if (object!=null){
            if (object instanceof JSONObject){
                return JSONObject.toJavaObject((JSONObject)object,tClass);
            }else if (object instanceof JSONArray){
                return JSONArray.toJavaObject((JSONArray)object,tClass);
            }else{
                return tClass.cast(object);
            }
        }
        return null;
    }
    public static <T> List<T> resToObjectListByClass(List<Object> objects, Class<T> tClass){
        return new ArrayList<T>(){{
            for (Object o:objects){
                add(resToObjectByClass(o,tClass));
            }
        }};
    }
}
